package com.bestow.loan.service.impl;

import java.util.List;

import com.bestow.common.utils.DateUtils;
import com.bestow.loan.domain.InvPurchaseRollOut;
import com.bestow.loan.service.IInvPurchaseRollOutService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bestow.loan.mapper.InvFinancePurchaseMapper;
import com.bestow.loan.domain.InvFinancePurchase;
import com.bestow.loan.service.IInvFinancePurchaseService;

/**
 * 申购/赎回管理Service业务层处理
 * 
 * @author liliu
 * @date 2022-09-04
 */
@Service
public class InvFinancePurchaseServiceImpl implements IInvFinancePurchaseService 
{
    @Autowired
    private InvFinancePurchaseMapper invFinancePurchaseMapper;

    @Autowired
    private IInvPurchaseRollOutService invPurchaseRollOutService;

    /**
     * 查询申购/赎回管理
     * 
     * @param financePurchaseId 申购/赎回管理主键
     * @return 申购/赎回管理
     */
    @Override
    public InvFinancePurchase selectInvFinancePurchaseByFinancePurchaseId(Long financePurchaseId)
    {
        return invFinancePurchaseMapper.selectInvFinancePurchaseByFinancePurchaseId(financePurchaseId);
    }

    /**
     * 查询申购/赎回管理列表
     * 
     * @param invFinancePurchase 申购/赎回管理
     * @return 申购/赎回管理
     */
    @Override
    public List<InvFinancePurchase> selectInvFinancePurchaseList(InvFinancePurchase invFinancePurchase)
    {
        return invFinancePurchaseMapper.selectInvFinancePurchaseList(invFinancePurchase);
    }

    /**
     * 新增申购/赎回管理
     * 
     * @param invFinancePurchase 申购/赎回管理
     * @return 结果
     */
    @Override
    public int insertInvFinancePurchase(InvFinancePurchase invFinancePurchase)
    {
        invFinancePurchase.setCreateTime(DateUtils.getNowDate());
        return invFinancePurchaseMapper.insertInvFinancePurchase(invFinancePurchase);
    }

    /**
     * 修改申购/赎回管理
     * 
     * @param invFinancePurchase 申购/赎回管理
     * @return 结果
     */
    @Override
    public int updateInvFinancePurchase(InvFinancePurchase invFinancePurchase)
    {
        invFinancePurchase.setUpdateTime(DateUtils.getNowDate());
        return invFinancePurchaseMapper.updateInvFinancePurchase(invFinancePurchase);
    }

    /**
     * 批量删除申购/赎回管理
     * 
     * @param financePurchaseIds 需要删除的申购/赎回管理主键
     * @return 结果
     */
    @Override
    public int deleteInvFinancePurchaseByFinancePurchaseIds(Long[] financePurchaseIds)
    {
        return invFinancePurchaseMapper.deleteInvFinancePurchaseByFinancePurchaseIds(financePurchaseIds);
    }

    /**
     * 删除申购/赎回管理信息
     * 
     * @param financePurchaseId 申购/赎回管理主键
     * @return 结果
     */
    @Override
    public int deleteInvFinancePurchaseByFinancePurchaseId(Long financePurchaseId)
    {
        return invFinancePurchaseMapper.deleteInvFinancePurchaseByFinancePurchaseId(financePurchaseId);
    }

    @Override
    public int saveInvFinancePurchase(InvFinancePurchase invFinancePurchase, List<InvPurchaseRollOut> invPurchaseRollOutList) {
        if(invFinancePurchase.getFinancePurchaseId()!=null){
            this.updateInvFinancePurchase(invFinancePurchase);
        }else{
            this.insertInvFinancePurchase(invFinancePurchase);
        }
        for(InvPurchaseRollOut invPurchaseRollOut:invPurchaseRollOutList){
            invPurchaseRollOut.setFinancePurchaseId(invFinancePurchase.getFinancePurchaseId());
            if (invPurchaseRollOut.getRollOutAccountId()!=null) {
                invPurchaseRollOutService.updateInvPurchaseRollOut(invPurchaseRollOut);
            }else{
                invPurchaseRollOutService.insertInvPurchaseRollOut(invPurchaseRollOut);
            }
        }
        return 1;
    }
}
